<script setup lang="ts">
import {
  useField,
  useForm,
  useValidateForm,
  useValidateField,
} from 'vee-validate'
import {
  accountRule,
  mobileRule,
  codeRule,
  passwordRule,
  rePasswordRule,
} from '@/utils/validate'
import { useMemberStore } from '@/store';
import { message } from '@/components/XtxUI';
import router from '@/router';
import { useCountDown } from '@/hooks';
const { unionId } = defineProps<{
  unionId: string;
}>();
// 表单校验

useForm({
  validationSchema: {
    account: accountRule,
    mobile: mobileRule,
    code: codeRule,
    password: passwordRule,
    repassword: rePasswordRule,
  },
})

const { errorMessage: accountError, value: account } =
  useField<string>('account')

const { errorMessage: passwordError, value: password } =
  useField<string>('password')
const { errorMessage: mobileError, value: mobile } = useField<string>('mobile')
const { errorMessage: codeError, value: code } = useField<string>('code')
const { errorMessage: repasswordError, value: repassword } =
  useField<string>('repassword')

const validForm = useValidateForm()
// 表单信息收集
// const form = reactive({
//   account: '',
//   mobile: '13012345729',
//   code: '123456',
//   password: '',
// });

const member = useMemberStore();
const loginBtn = async () => {
  // 表单校验
  const res = await validForm()
  if (!res.valid) return
  // member.registerLogin(form, unionId);
  await member.registerLogin({
    mobile: mobile.value,
    code: code.value,
    account: account.value,
    password: password.value,
  }, unionId);
  console.log(111);

  message({ type: 'success', text: '注册成功' })
  router.push('/')
};
// 获取验证码 单独校验手机号
const validMobile = useValidateField('mobile')
const { count, start } = useCountDown()
const send = async () => {
  if (count.value > 0) return
  // console.log('获取验证码')
  // 单独校验手机号
  const res = await validMobile()
  if (!res.valid) {
    return
  }
  await member.sendQQBindMsg(mobile.value)
  message({ type: 'success', text: '获取验证码成功' })

  start(60)
}
</script>

<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model.trim="account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error">{{ accountError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model.trim="mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error">{{ mobileError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model.trim="code" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="send">{{
        count === 0 ? '发送验证码' : `${count}s后发送`
        }}</span>
      </div>
      <div class="error">{{ codeError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model.trim="password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error">{{ passwordError }}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" v-model.trim="repassword" type="password" placeholder="请确认密码" />
      </div>
      <div class="error">{{ repasswordError }}</div>
    </div>
    <a @click="loginBtn" href="javascript:;" class="submit">立即提交</a>
  </div>
</template>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;

  &:hover {
    cursor: pointer;
  }
}
</style>
